<?php
include_once("adminBase.php");

class Setsys extends AdminBase{
	function index()
	{
		$data = $this->getSysInfo();
		$this->smarty->assign("sysInfo",$data);
		adminDisplay("admin_setsys.tpl");
	}
	
	function save()
	{
		$sql = "";
		$errorMsg = array();
		$auditing = intval($_REQUEST['auditing']);
		$old_auditing = intval($_REQUEST['old_auditing']);
		if($auditing != $old_auditing){
			$sql .= "update tbl_system set value='".$auditing."' where name='auditing';";
		}
		$registemailcheck = intval($_REQUEST['registemailcheck']);
		$old_registemailcheck = intval($_REQUEST['old_registemailcheck']);
		if($registemailcheck != $old_registemailcheck){
			$sql .= "update tbl_system set value='".$registemailcheck."' where name='registemailcheck';";
		}
		$forviewnum = intval($_REQUEST['forviewnum']);
		$old_forviewnum = intval($_REQUEST['old_forviewnum']);
		if($forviewnum != $old_forviewnum){
			if($forviewnum > 0){
				$sql .= "update tbl_system set value='".$forviewnum."' where name='forviewnum';";
			}else{
				$_SESSION['sysMsg'] = "前台显示记录数不合法";
			}
		}
		$backviewnum = intval($_REQUEST['backviewnum']);
		$old_backviewnum = intval($_REQUEST['old_backviewnum']);
		if($backviewnum != $old_backviewnum){
			if($backviewnum > 0){
				$sql .= "update tbl_system set value='".$backviewnum."' where name='backviewnum';";
			}else{
				$_SESSION['sysMsg'] = "后台显示记录数不合法";
			}
		}
		$visitor = trim($_REQUEST['visitor']);
		$old_visitor = trim($_REQUEST['old_visitor']);
		if($visitor != $old_visitor){
			if($visitor != ""){
				$sql .= "update tbl_system set value='".$visitor."' where name='visitor';";
			}else{
				$_SESSION['sysMsg'] = "游客名称不合法";
			}
		}
		if($sql != ""){
			$this->db->query($sql);
		}
		if(empty($_SESSION['sysMsg'])){
			$_SESSION['sysMsg'] = "更新成功";
		}
		$this->updateDefineFile();
		$this->index();
	}

	function getSysInfo()
	{
		$sql = "select * from tbl_system";
		$this->db->setQuery($sql);
		$data = $this->db->loadResultList("name","value");
		return $data;
	}

	function updateDefineFile()
	{
		$sysinfo = $this->getSysInfo();
		$filestr = "<?php \r\n";
		foreach($sysinfo as $key=>$value)
		{
			$filestr .= "define('_".strtoupper($key)."','".$value."');";
		}
		$file = _INC_PATH."define.php";
		writeFile($filestr,$file);
	}
}

$setsys = new Setsys();